// Handle combobox
App.directive('edCombobox', ['Page','$rootScope', function (Page,$rootScope) {
    return {
        restrict: 'A',
        scope: {
            model : '@ngModel'
        },
        require: 'ngModel',
		link : function (scope, elem, attrs, ngModelController) {
			var option = AppUtil.jsonfy(attrs.options);
			// 兼容老的方式
			option.url = option.url || attrs.edUrl;
			if (!option.idField) {
				var arr = attrs.edKey.split(',');
				option.idField = arr[0];
				option.textField = arr[1];
			}

			Page.ajaxGetQuiet(option.url, null, function(resp) {
				var rows = resp.data;
				if (resp.data.rows) {
					rows = resp.data.rows;
				}
        		elem.append('<option value="" selected="selected">请选择</option>');
        		$(rows).each(function(index, data) {
        			elem.append('<option value="' + data[option.idField] + '" >' + data[option.textField] + '</option>');
        		});
       	 	});
			ngModelController.$render = function() {
				$(elem).val("");
				$(elem).val(ngModelController.$viewValue );
			}
		}
    };
}]);
